<?php
/**
 * Created by PhpStorm.
 * Author: 芸签cms www.yunqiancms.com
 * User: sun
 * Date: 2022/10/31
 * Time: 10:33 AM
 */
namespace app\frontend\modules\site\services;

use app\common\models\Category;
use app\common\models\Product;
use app\common\models\Theme;
use app\common\exceptions\BizException;

class ProductService
{
    public function getList($params){
        $query = Product::site()->select();
        if($params['cid']){
            $query->where('cid',$params['cid']);
        }
        if($params['name']){
            $query->where('name','like','%'.$params['name'].'%');
        }
        $list = $query
            ->where('status',1)
            ->with(['category' => function($query) {
                $query->select();
            }])
            ->orderBy('id', 'desc')
            ->paginate(8);
        $list->map(function ($v){
            $v->cname = '';
            if($v['category']&&$v['category']['name']){
                $v->cname = $v['category']['name'];
            }
        });

        return $list;
    }

    public function getHot(){
        $list = Product::site()->select()
            ->where('is_hot',1)
            ->limit(3)
            ->orderBy('id', 'desc')
            ->get();
        return $list;
    }

    /**
     * 列表合并SEO信息
     * @param $siteInfo
     * @param $categoryInfo
     * @return mixed
     */
    public function fmtListSeoInfo($siteInfo,$categoryInfo){
        $siteInfo['seo_title'] = $categoryInfo['name'].'-'.$siteInfo['seo_title'];
        $siteInfo['seo_keywords'] = $categoryInfo['keywords']?$categoryInfo['keywords']:$siteInfo['seo_keywords'];
        $siteInfo['seo_description'] = $categoryInfo['description']?$categoryInfo['description']:$siteInfo['seo_description'];
        return $siteInfo;
    }

    /**
     * 列表模版文件
     * @param $clientType
     * @param $view
     * @param $categoryInfo
     * @return mixed
     */
    public function getListView($clientType,$view,$categoryInfo){
        if($clientType==Theme::TYPE_PC){
            if($categoryInfo['pc_list_tpl']){
                $view = $categoryInfo['pc_list_tpl'];
            }
        }
        if($clientType==Theme::TYPE_H5){
            if($categoryInfo['h5_list_tpl']){
                $view = $categoryInfo['h5_list_tpl'];
            }
        }

        return $view;
    }

    /**
     * 使用某模版的所有分类
     * @param $clientType
     * @param $view
     * @return mixed
     */
    public function getCategroyListByView($clientType,$view){
        $query = Category::select();
        if($clientType==Theme::TYPE_PC){
            $query = $query
                ->where('pc_list_tpl',$view);
        }
        if($clientType==Theme::TYPE_H5){
            $query = $query
                ->where('h5_list_tpl',$view);
        }
        $list = $query
            ->orderBy('display_order','desc')
            ->orderBy('id','desc')
            ->get();
        return $list;
    }
    /**
     * 产品详情
     * @param $id
     * @return array
     */
    public function getDetail($id){
        $model = Product::site()->select()
            ->with(['channel' => function($query) {
                return $query->select();
            }])
            ->where('id',$id)
            ->first();
        $res = [];
        $model->images = json_decode($model->images,true);
        foreach ($model->images as $k=>$v){
            $images_url[] = fmt_media_url($v);
        }
        $model->images_url = $images_url;
        if ($model->status == 0){
            throw new BizException('该产品状态为未开启');
        }else{
            $res['info'] = $model;
        }
        return $res;
    }

    /**
     * 产品详情合并SEO信息
     * @param $siteInfo
     * @param $productInfo
     * @return mixed
     */
    public function fmtDetailSeoInfo($siteInfo,$productInfo){
        $siteInfo['seo_title'] = $productInfo['title'].'-'.$siteInfo['seo_title'];
        if($productInfo['keywords']){
            $siteInfo['seo_keywords'] = $productInfo['keywords'];
        }else if($productInfo['category']['keywords']){
            //产品没设置，取列表的
            $siteInfo['seo_keywords'] = $productInfo['category']['keywords'];
        }
        if($productInfo['description']){
            $siteInfo['seo_description'] = $productInfo['description'];
        }else if($productInfo['category']['description']){
            //产品没设置，取列表的
            $siteInfo['seo_description'] = $productInfo['category']['description'];
        }
        return $siteInfo;
    }

    /**
     * 产品详情模版文件
     * @param $clientType
     * @param $view
     * @param $productInfo
     * @return mixed
     */
    public function getDetailView($clientType,$view,$productInfo){
        if($productInfo['use_tpl']){
            //使用独立模版
            if($clientType==Theme::TYPE_PC){
                if($productInfo['pc_tpl']){
                    $view = $productInfo['pc_tpl'];
                }
            }
            if($clientType==Theme::TYPE_H5){
                if($productInfo['h5_tpl']){
                    $view = $productInfo['h5_tpl'];
                }
            }
        }else{
            //使用对应分类的模版
            if($clientType==Theme::TYPE_PC){
                if($productInfo['category']['pc_detail_tpl']){
                    $view = $productInfo['category']['pc_detail_tpl'];
                }
            }
            if($clientType==Theme::TYPE_H5){
                if($productInfo['category']['h5_detail_tpl']){
                    $view = $productInfo['category']['h5_detail_tpl'];
                }
            }
        }
        return $view;
    }
}
